unit MainCtrl;

interface
uses
  SysUtils, forms, buttons, classes, menus, controls, patterns,
  MainMdl, MainView;

type
  TControllerMain = class(TController)
    model: TModelMain;
    view: TViewMain;
  protected
    procedure DoCommand(ACommand: string; const args: TObject = nil); override;

  public
    constructor Create;
    destructor Destroy; override;
    procedure OnClick(Sender: TObject); //for ieventListener
    procedure OnMouseEvent(Sender: TObject;
      Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  end;

implementation
uses
  commandlist;

procedure TControllerMain.OnClick(Sender: TObject);
begin
  if not assigned(sender) then
    exit;

  if (sender is Tmenuitem) then
  begin
    if (TMenuItem(Sender).caption = '&About...') then
      self.sendCommand(CMD_SHOW_ABOUT)
    else if (TMenuItem(Sender).caption = '&Help') then
      application.MessageBox('Help menu selected!', 'Help', 0);
  end;
end;

procedure TControllerMain.OnMouseEvent(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin

end;

constructor TControllerMain.Create;
begin
  inherited;
  model := TModelMain.Create;
  Application.CreateForm(TViewMain, view);
  view.setNotifyEventHandler(self.onClick);
  model.RegObserver(view);
end;

destructor TControllerMain.destroy;
begin
  freeAndNil(model);
  inherited;
end;



procedure TControllerMain.DoCommand(ACommand: string; const args: TObject = nil);
begin
  if ACommand = CMD_SYSTEM_START then
  begin
    self.SendCommand(CMD_REQUIRE_INDEXTREE);
    self.SendCommand(CMD_REQUIRE_FATHERVIEW);
    self.SendCommand(CMD_REQUIRE_MOTHERVIEW);
    self.SendCommand(CMD_REQUIRE_CHILDRENVIEW);
  end
  else if (ACommand = CMD_SHOW_INDEXTREE) or
    (ACommand = CMD_SHOW_FATHERVIEW) or
    (ACommand = CMD_SHOW_MOTHERVIEW) or
    (ACommand = CMD_SHOW_CHILDRENVIEW) then
  begin
    //the args must be TFrame
    model.setData(ACommand, args);
  end
  else if (ACommand = CMD_NODE_SELECT) and assigned(args) then
    model.setData(ACommand, args);

end;

initialization
  ControlCenter.RegController(TControllerMain.Create);

end.

